home *** CD-ROM | disk | FTP | other *** search
- Path: hubcap.clemson.edu!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.lang.c
- Subject: Re: Problem...Problem!
- Date: 1 Feb 96 00:29:13 GMT
- Organization: Clemson University
- Message-ID: <mjs.823134553@hubcap>
- References: <4el09q$6im@ratree.psu.ac.th>
- NNTP-Posting-Host: hubcap.clemson.edu
-
- s3610165@maliwan.psu.ac.th (Sanon CHAOCHAIYAPORN) writes:
-
- >Dear all
- > This is a iteration method program. It use to find three values,
- >a b and c. This program will finish when variables, t[], are less than or
- >equal an error value, ERR. But my program has not been runing. Please,
- >advice me...:~( Thanks for your attention.
-
- >Source:
- >#include<stdio.h>
- >#include<conio.h>
-
- >#define NUM 8
- >#define ERR 1e-4
-
- >float v[NUM];
- >int i;
-
- No need for this declaration of i. It is initialized to zero and used
- only in the function display, where it is never set. It's a good example
- of why global variables are a good thing to avoid (in general).
-
- >main()
- >{
- > float e[NUM],t[NUM];
- > int i;
- > char pass;
-
- The variable pass is never used.
-
- > clrscr();
- > for(i=0;i<NUM;i++)
- > v[i] = e[i] = t[i] = 0;
- > do
- > {
- > i = 0;
-
- In fact, why not just substitute out the i from the following expressions:
-
- > v[i] = (v[i+1] + v[i+2]) / 8;
- > v[i+1] = (v[i] + v[i+3] + 40) / 8;
- > v[i+2] = (v[i] + v[i+3] + v[i+5] + 50) / 8;
- > v[i+3] = (v[i+1] + v[i+2] + 110) / 8;
- > v[i+4] = (v[i+5] + v[i+6] + 150) / 8;
- > v[i+5] = (v[i+2] + v[i+4] + v[i+7] + 70) / 8;
- > v[i+6] = (v[i+4] + v[i+7] + 200) / 8;
- > v[i+7] = (v[i+5] + v[i+6] + 200) / 8;
-
- v[0] = (v[1] + v[2]) / 8;
- v[1] = (v[0] + v[3] + 40) / 8;
- v[2] = (v[0] + v[3] + v[5] + 50) / 8;
- v[3] = (v[1] + v[2] + 110) / 8;
- v[4] = (v[5] + v[6] + 150) / 8;
- v[5] = (v[2] + v[4] + v[7] + 70) / 8;
- v[6] = (v[4] + v[7] + 200) / 8;
- v[7] = (v[5] + v[6] + 200) / 8;
-
- Are the constants derived from some formula? Is the 8 from NUM? Is
- there some formula that tells what each line should be as a function
- of NUM and the element of v[] that is being computed? If so, you
- could do this calculation in a loop, and make it independent of NUM.
-
- > display();
- > for(i=0;i<NUM;i++)
- > t[i] = v[i] - e[i];
- > for(i=0;i<NUM;i++)
- > e[i] = v[i];
- > }while((t[0] <= ERR) && (t[1] <= ERR) && (t[2] <= ERR) && (t[3] <= ERR) &&
- > (t[4] <= ERR) && (t[5] <= ERR) && (t[6] <= ERR) && (t[7] <= ERR));
-
- This loop is executed only as long as all values of t[] are less than
- ERR. Since this is not the case the first time through the loop, the
- loop exits after the first pass. You probably want the negation of
- this condition. Also, you could test the condition in a loop, to
- make it independent of NUM.
-
- > printf("\na is %8.4f volt",v[1]);
- > printf("\nb is %8.4f volt",v[2]);
- > printf("\nc is %8.4f volt",v[4]);
-
- Add
- printf("\n");
-
- return 0;
- >}
-
- >display()
- >{
- > printf("%10.4f",v[i]);
- > printf("%10.4f",v[i+1]);
- > printf("%10.4f",v[i+2]);
- > printf("%10.4f",v[i+3]);
- > printf("%10.4f",v[i+4]);
- > printf("%10.4f",v[i+5]);
- > printf("%10.4f",v[i+6]);
- > printf("%10.4f",v[i+7]);
- >}
-
- The i in this routine is that global i, initialized to zero. Why not
- substitute it out? Better yet, why not do this in a loop? Also,
- since display does not return a value, you should declare it as
- returning void (although maybe that's advanced for you at this point).
-
- void display()
- {
- int i;
-
- for ( i = 0; i < NUM; i++ )
- printf("%10.4f ", v[i]);
-
- printf("\n");
- }
-
- If I were writing this, I'd pass v[] (and NUM) as a parameter to
- display(), but maybe that's advanced for you also.
-
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-